package com.youths.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import com.youths.domain.LabelAdmin;
import com.youths.domain.LabelConsumer;
import com.youths.response.Result;
import com.youths.service.ConsumerInfoService;
import com.youths.service.LoginService;
import com.youths.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
@Slf4j
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    ConsumerInfoService consumerInfoService;

    @GetMapping(value = "login")
    public String login() {
        return "login";
    }

    @GetMapping(value = "cancel")
    public String cancel(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }

    @PostMapping(value = "consumerLogin")
    public String ConsumerLogin(@RequestParam("phone") String phone,
                                @RequestParam("password") String password,
                                HttpSession session,Model model) {
        try {
            LabelConsumer labelConsumer = loginService.consumerLogin(phone, password);
            if(labelConsumer != null){
                session.setMaxInactiveInterval(30*60);
                session.setAttribute("user", labelConsumer);
                //return "youthInfo/list";
                return "redirect:/main";
            }
            else{
                model.addAttribute("msg","用户名或密码错误");
            }
        } catch (Exception e) {
            model.addAttribute("msg","用户名或密码错误");
            e.printStackTrace();
        }

        return "login";
    }

    @GetMapping(value = "/password/edit")
    @ResponseBody
    public String editPassword(String oldPassword, String newPassword, String checkPassword,
                               HttpSession session) {
        Gson gson = new Gson();
        Map<String, Object> resMap = new HashMap<>();
        LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
        if (ObjectUtil.isNotNull(consumer)) {
            if (StrUtil.equals(consumer.getPassword(), MD5Util.MD5("youths" + oldPassword))) {
                if (newPassword.equals(checkPassword)) {
                    if (newPassword.length() >= 6 && newPassword.length() <= 12) {
                        loginService.updatePassword(consumer.getConsumerId(), MD5Util.MD5("youths" + newPassword));
                        resMap.put("res", 1);
                    }else {
                        resMap.put("res", 0);
                        resMap.put("msg", "密码长度应在6-12字符之间");
                    }
                }else {
                    resMap.put("res", 0);
                    resMap.put("msg", "两次密码不一致");
                }
            }else {
                resMap.put("res", 0);
                resMap.put("msg", "密码验证不通过");
            }
        }else {
            resMap.put("res", 0);
            resMap.put("msg", "没有登录");
        }
        return gson.toJson(resMap);
    }


    @GetMapping(value = "/back/adminLogin")
    @ResponseBody
    public Result adminLogin(@RequestParam("account") String account,
                             @RequestParam("password") String password){
        return Result.success(loginService.adminLogin(account,password));
    }

}